package hu.szyrt.bug.adapters;

import hu.szyrt.bug.R;
import hu.szyrt.bug.models.Category;
import hu.szyrt.bug.utils.Assets;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryWidgetAdapter extends BaseAdapter{

	private List<Category> items = new ArrayList<Category>();	
	private ViewHolder holder;
	private Assets assets;
	
	public CategoryWidgetAdapter(Assets assets, List<Category> items) {
		this.assets = assets;			
		this.items = items;
	}
	
	@Override
	public int getCount() { return items.size(); }
	@Override
	public Object getItem(int position) { return items.get(position); }
	@Override
	public long getItemId(int position) { return position; }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Category item = items.get(position);
		
		if(convertView == null){
			convertView = View.inflate(parent.getContext(), R.layout.category_widget, null);
			holder = new ViewHolder();
			holder.icon = (ImageView)convertView.findViewById(R.id.icon);
			holder.text = (TextView)convertView.findViewById(R.id.title);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		holder.icon.setImageBitmap(assets.loadCategoryIcon(item.getImageName(), 1));
		holder.text.setText(item.getName());
		
		return convertView;			
	}
	
	static class ViewHolder{
		ImageView icon;
		TextView text;
	}
	
}
